<?php 

namespace Admin\Controller;

// +----------------------------------------------------------------------
// | 文件 ManualController.class.php
// +----------------------------------------------------------------------
// | 说明 人工操作的控制
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------


class ManualController extends AdminController {

    // 随机点赞功能
    // type: video/user  对视频或者人点赞   count 是几个人点赞/ 0 为随机
    public function randomZan($type='video', $count = 0)
    {
        if (IS_POST){
            
            if ($count == 0){
                $count = rand(3, 10);
            }
            $userModel = D('Common/Member');
            $videoModel = service('Video');

            $map = [
                'avatar' => ['neq', ''],  // 头像非空
                'nickname' => ['neq', ''], // 昵称非空
                'last_login_time' => ['lt', NOW_TIME - 3*30*24*3600], // 3月前
            ];
            // 操作用户
            $res = $userModel->where($map)->getField('uid', true);
            shuffle($res);
            $uids = array_slice($res, 0, $count);
            $target_list = 30;
            switch ($type) {
                case 'video':
                    $likeType = 0;
                    $where['status'] = $videoModel::STATUS_PASS;
                    $where['race_id'] = array('$eq' => null);
                    // 最近的$target_list条视频 随机抽选
                    $res = $videoModel->where($where)->order('id desc')->limit($target_list)->getField('id', true);
                    break;
                case 'user':
                    $likeType = 2;
                    // 近期登录的用户 $target_list个 随机抽选
                    $res = $userModel->where(['nickname' => ['neq', '']])->order('last_login_time desc')->limit($target_list)->getField('uid', true);
                    break;
                default:
                    return $this->error('操作类型错误');
                    break;
            }
            if ($res){
                shuffle($res);
                foreach ($uids as $key => $uid) {
                    $target_id = $res[$key];
                    $ok = event('Like')->add($likeType, $uid, $target_id);
                }
            }
            $this->success('操作完成');
        }else{
            $this->assign('types', ['video', 'user']);
            $this->display();
        }
    }

    // 随机增加视频的播放
    public function videoPlay()
    {
        if (IS_POST){
            $date = date('Y-m-d'); // 当天凌晨
            $datestr = strtotime($date);
            // 找出5天内发布的视频
            $map['create_time'] = [
                'between', [  $datestr - 5*24*3600  , $datestr ]
            ];

            $map['status'] = 1;
            $map['type']   = 0;
            $map['race_id'] = array('egt', null);

            $videos = service('Video')->where($map)->select();
            $result = [];
            foreach ($videos as $video) {
                
                switch (  ceil(($datestr - $video['create_time'])/(24*3600) ) ) {
                    case 1:
                        $range = [150,300];
                        break;
                    case 2:
                        $range = [100,200];
                        breank; 
                    case 3:
                        $range = [50,150];
                        breank;
                    case 4:
                        $range = [30,100];
                        breank;
                    case 5:
                        $range = [10,50];
                        breank;            
                    default:
                        continue;
                        break;
                }
                $addplay = mt_rand($range[0], $range[1]);
                event('Video')->setPlay($video['id'], $addplay);
                $result[] = [
                    'video' => $video,
                    'add'   => $addplay
                ];
                $total['video']++;
                $total['add']+=$addplay;
            }
            $this->assign('result', $result);
            $this->assign('total', $total);
            $this->display();
        }else{
            $this->display();
        }
    }


}